﻿using System;
using System.Collections.Generic;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;
using System.Web.UI;
using shfa.snomtastic.data;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eConfigurations : EditBase
    {
        #region Public Members


        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {

            BindGridItems();

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);
            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "ClientEvents", common.DblClickScript, true);
            
        }

        #endregion

       
        #region Event Handlers

        protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {

            if (e.CommandName == "Edit")
            {
                e.Canceled = true;
                Guid id = new Guid(rgListItems.Items[e.Item.ItemIndex].GetDataKeyValue("ConfigurationId").ToString());
                RaiseNavEvent(this, new NavEventArgs("eConfigurationSettings", id));
            }
        }

        #endregion

      

      

        


        public void BindGridItems()
        {
            ConfigurationController cc = new ConfigurationController();
            List<ConfigurationInfo> cList = cc.SelectConfigurations("Name Asc");


            rgListItems.DataSource = cList;
            rgListItems.DataBind();


        }


        #region Action Events

        public void CopyItem(Guid id)
        {
            ConfigurationController cc = new ConfigurationController();
            ConfigurationInfo ci = cc.SelectConfiguration(id);
            if (ci != null)
            {
                ci.Name = ci.Name + " copy";
                ci.IsDefault = false;
                Guid newId = cc.InsertConfiguration(ci);
                ConfigurationSettingController csc = new ConfigurationSettingController();
                List<ConfigurationSettingInfo> csiList = csc.SelectConfigurationSettingsByConfiguration(id);
                foreach (ConfigurationSettingInfo csi in csiList)
                {
                    csi.ConfigurationId = newId;
                    csc.InsertConfigurationSetting(csi);
                }
                RaiseNavEvent(this, new NavEventArgs("eConfigurations", newId));

            }

        }

        public void DeleteItem(Guid id)
        {
            ConfigurationController cc = new ConfigurationController();
            ConfigurationInfo ci = cc.SelectConfiguration(id);
            if (ci != null)
            {
                if (!ci.IsDefault)
                {
                    cc.DeleteConfiguration(id);
                    RaiseNavEvent(this, new NavEventArgs("eConfigurations", id));
                }
            }

        }


        #endregion

       



        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Add":
                    RaiseNavEvent(this, new NavEventArgs("eConfiguration", new Guid()));
                    break;
                case "Edit":
                    if (rgListItems.SelectedValue != null)
                    {
                        RaiseNavEvent(this, new NavEventArgs("eConfiguration", new Guid(rgListItems.SelectedValue.ToString())));
                    }
                    break;
                case "Copy":
                    {
                        CopyItem(new Guid(rgListItems.SelectedValue.ToString()));
                    }
                    break;
                case "Settings":
                    if (rgListItems.SelectedValue != null)
                    {
                        RaiseNavEvent(this, new NavEventArgs("eConfigurationSettings", new Guid(rgListItems.SelectedValue.ToString())));
                    }
                    break;
               
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Add", "Add Configuration", "~/images/icons/add.png"));
                al.Add(new ActionInfo("Edit", "Edit Configuration", "~/images/icons/edit.png"));
                al.Add(new ActionInfo("Copy", "Copy Configuration", "~/images/icons/copy.png"));
                al.Add(new ActionInfo("Settings", "Change Settings", "~/images/icons/configsetting.png", "", true, false));
                return al;
            }
        }

        #endregion
    }
}